<?php
/**
 * Created by LíJimpéy(：
 * User: jimpey88
 * Date: 2024/04/10
 * Email: < jimpey@qq.com >
 * Project: company-admin-tp6
 */
declare (strict_types = 1);

namespace app\backend_api\validate;

use app\backend_api\model\Articles;
use app\common\validate\BaseValidate;

class ArticleValidate extends BaseValidate
{
    /**
     * 定义验证规则
     * 格式：'字段名' =>  ['规则1','规则2'...]
     *
     * @var array
     */
    protected $rule = [
        'title' => 'require|min:3|max:30',
        'sort_name' => 'require|min:3|max:10',
        'thumbnail' => 'require|max:500',
        'banner' => 'require|max:500',
        'is_top' => 'boolean'
    ];

    /**
     * 定义错误信息
     * 格式：'字段名.规则名' =>  '错误信息'
     *
     * @var array
     */
    protected $message = [
        'title.createUnique' => '标题已存在',
        'title.editUnique' => '标题已存在',
    ];

    protected function sceneCreate()
    {
        return $this->append('title', 'createUnique');
    }

    protected function sceneEdit()
    {
        return $this->append('title', 'editUnique');
    }

    // 自定义验证规则
    protected function createUnique($value,$rule,$data=[],$field)
    {
        if (Articles::where('title',$value)->count()) {
            return false;
        }
        return true;
    }

    protected function editUnique($value,$rule,$data=[],$field)
    {
        if (Articles::where([['id','<>', $data['id']],['title','=',$value]])->count()) {
            return false;
        }
        return true;
    }

}
